package org.hxzon.rpggame.admcmd;

import io.netty.channel.ChannelHandlerContext;

import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.hxzon.rpggame.consts.AdmChannelDataConsts;
import org.hxzon.rpggame.core.AdmCmd;
import org.hxzon.rpggame.core.impl.AbstractAdmCmdImpl;
import org.hxzon.rpggame.util.ScriptEngineUtil;
import org.hxzon.util.Data;
import org.hxzon.util.StringParser;

public class TestAdmCmd extends AbstractAdmCmdImpl implements AdmCmd {

    public TestAdmCmd() {
        super("test");
    }

    @Override
    protected Options buildOptions() {
        Option help = new Option("help", "print usage");
        Option m = new Option("m", "more");
        Option quit = new Option("q", "quit");
        @SuppressWarnings("static-access")
        Option ao = OptionBuilder//
                .hasArg()//
                .withDescription("only one arg")//
                .create("ao");
        @SuppressWarnings("static-access")
        Option am = OptionBuilder.withArgName("file1,file2,...")//
                .hasArgs()//
                .withDescription("more args")//
                .create("am");
        @SuppressWarnings("static-access")
        Option property = OptionBuilder.withArgName("property=value")//
                .hasArgs(2)//
                .withValueSeparator()//
                .withDescription("use value for given property")//
                .create("D");
        //
        Options options = new Options();

        options.addOption(help);
        options.addOption(m);
        options.addOption(quit);
        options.addOption(ao);
        options.addOption(am);
        options.addOption(property);
        return options;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(StringParser parser, ChannelHandlerContext ctx) {
        //test -help
        //test -m
        //test -Dk1=v1 -am m1 m2 m3 -ao o1 def -Dk2=v2 -Dk2=v3 -ao o2 abc efg
        String cmdLine = parser.getStr().toString();
        CommandLine line = parseCmd(cmdLine, ctx);
        if (line.hasOption("help")) {
            ctx.writeAndFlush(usage());
            return;
        }
        if (line.hasOption('q')) {
            Data data = (Data) ScriptEngineUtil.invoke("getChannelData", ctx.channel());
            data.remove(AdmChannelDataConsts.CurCmd);
            return;
        }
        if (line.hasOption('m')) {
            Data data = (Data) ScriptEngineUtil.invoke("getChannelData", ctx.channel());
            data.put(AdmChannelDataConsts.CurCmd, this);
            ctx.writeAndFlush("more:\r\n");
            return;
        }
        for (String arg : (List<String>) line.getArgList()) {
            ctx.writeAndFlush(arg + "\r\n");
        }
        if (line.hasOption("ao")) {
            String firstValue = line.getOptionValue("ao");
            ctx.writeAndFlush(firstValue + "\r\n");
            String[] values = line.getOptionValues("ao");
            for (String value : values) {
                ctx.writeAndFlush(value + "\r\n");
            }
        }
        if (line.hasOption("am")) {
            String firstValue = line.getOptionValue("am");
            ctx.writeAndFlush(firstValue + "\r\n");
            String[] values = line.getOptionValues("am");
            for (String value : values) {
                ctx.writeAndFlush(value + "\r\n");
            }
        }
        if (line.hasOption('D')) {
            String[] values = line.getOptionValues("D");
            for (int i = 0; i < values.length; i += 2) {
                ctx.writeAndFlush(values[i] + "=" + values[i + 1] + "\r\n");
            }
        }
    }

    @Override
    public void executeContinue(StringParser cmdLine, ChannelHandlerContext ctx) {
        execute(cmdLine, ctx);
    }
}
